package edu.iss.demo.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Subquery;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.iss.demo.model.Employee;
import edu.iss.demo.model.LeaveApplication;
import edu.iss.demo.repository.LeaveApplicationRepository;

@Service
public class LeaveApplicationServiceImpl implements LeaveApplicationService {

	@Resource
	private LeaveApplicationRepository leaveAppRep;
	
	@PersistenceContext
    private EntityManager em;
	
	@Override
	@Transactional
	public LeaveApplication create(LeaveApplication la) {
		// TODO Auto-generated method stub
		LeaveApplication createdLeaveApp = la;
		return leaveAppRep.save(createdLeaveApp);
	}

	@Override
	@Transactional
	public List<LeaveApplication> findAll() {
		// TODO Auto-generated method stub
		return leaveAppRep.findAll();
		
	}

	@Override
	@Transactional
	public LeaveApplication findbyid(int Leaveid) {
		// TODO Auto-generated method stub
		return leaveAppRep.findOne(Leaveid);
	}
	
	//this method is searching all the leave applications by status
	@Override
	@Transactional
	@SuppressWarnings("unchecked")
	public List<LeaveApplication> findLeaveAppByStatus(String status)
	{
		
		 return em.createQuery("SELECT la FROM LeaveApplication la WHERE la.status =:status").setParameter("status", status).getResultList();
	}

	@Override
	@Transactional
	@SuppressWarnings("unchecked")
	public ArrayList<LeaveApplication> findEmployeeList(String mId)
	{
		List<Employee> list = em.createQuery("SELECT e FROM Employee e WHERE e.managerId=:mId").setParameter("mId",mId).getResultList();
		ArrayList<LeaveApplication> leaveList = new ArrayList<LeaveApplication>();
		for(Employee e: list)
		{    String i = e.getEmployeeId();
			List<LeaveApplication> laList = em.createQuery("SELECT la FROM LeaveApplication la WHERE la.employee.employeeId=:employeeId").setParameter("employeeId", i).getResultList();
			for(LeaveApplication la : laList)
			{
				leaveList.add(la);
			}
		}
		return leaveList;
	}
	
	
	@Override
	@Transactional
	public LeaveApplication update(LeaveApplication la) {
		// TODO Auto-generated method stub
		//System.out.println(la.getLeaveId());
		
			LeaveApplication updatedStudent = leaveAppRep.findOne(la.getLeaveId());
			
		/*	if (updatedStudent== null)
				throw new StudentNotFound();*/
			
			updatedStudent.setMangerComment(la.getMangerComment());
			updatedStudent.setStatus("APPROVED");
			leaveAppRep.flush();
			return updatedStudent;
	
	}


	//here need to ask
	@Override
	@Transactional
	@SuppressWarnings("unchecked")
	public List<LeaveApplication> findLeaveAppByStatusNEmpId(String status,String employee) {
		return em.createQuery("SELECT la FROM LeaveApplication la WHERE la.status =:status AND la.employee.employeeId =:employee").setParameter("status",status).setParameter("employee",employee).getResultList();
	}

	@Override
	@Transactional
	public LeaveApplication updateReject(LeaveApplication la) {
		// TODO Auto-generated method stub
		System.out.println("ID ------->"+la.getLeaveId());
		LeaveApplication updatedStudent = leaveAppRep.findOne(la.getLeaveId());
		updatedStudent.setMangerComment(la.getMangerComment());
		updatedStudent.setStatus("REJECTED");
		leaveAppRep.flush();
		return updatedStudent;
	}

	@Override
	public List<LeaveApplication> ReqManager(String empID) {
		// TODO Auto-generated method stub
		return null;
	}

	
	//this is for employee to update his or her pending leave application
	@Override
	@Transactional
	public LeaveApplication empUpdate(LeaveApplication la) {
			
		LeaveApplication updatedLeaveApp = leaveAppRep.findOne(la.getLeaveId());
			
		updatedLeaveApp.setLeaveType(la.getLeaveType());
		updatedLeaveApp.setFromDate(la.getFromDate());
		updatedLeaveApp.setToDate(la.getToDate());
		updatedLeaveApp.setEmployeeComment(la.getEmployeeComment());
		
		leaveAppRep.flush();
		return updatedLeaveApp;
			
			
	}
	

}
